package com.yuesystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuesystem.common.BaseContext;
import com.yuesystem.common.CustomException;
import com.yuesystem.common.R;
import com.yuesystem.dto.responsebody.ApplyResponseBody;
import com.yuesystem.service.ApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 申请类.
 */
@RestController
@RequestMapping("/apply")
@Slf4j
@CrossOrigin
public class ApplyController {
  @Autowired
  private ApplyService applyService;

  /**
   * 用户申请成为编辑.
   *
   * @return 成功与否的提示信息.
   */
  @PostMapping("/toEdit")
  public R<String> apply1() {
    int status = BaseContext.getCurrentUser().getStatus();
    if (status == 3) {
      throw new CustomException("您已无权限申请客服");
    }
    if (status == 1) {
      throw new CustomException("已是编辑，请勿重复申请!");
    }

    Long userId = BaseContext.getCurrentId();
    applyService.add(userId);
    return R.success("申请成功！");
  }

  /**
   * 用户申请成为客服.
   *
   * @return 成功与否的提示信息.
   */
  @PostMapping("/toServ")
  public R<String> apply2() {
    int status = BaseContext.getCurrentUser().getStatus();
    if (status == 1) {
      throw new CustomException("您已无权限申请编辑");
    }
    if (status == 3) {
      throw new CustomException("已是客服，请勿重复申请!");
    }

    Long userId = BaseContext.getCurrentId();
    applyService.add(userId);
    return R.success("申请成功！");
  }

  /**
   * 分页展示用户申请信息.
   *
   * @param page     起始页
   * @param pageSize 页数
   * @return 用户申请类的分页形式
   */
  @GetMapping("/list")
  public R<Page<ApplyResponseBody>> getApply(int page, int pageSize) {
    int status = BaseContext.getCurrentUser().getStatus();
    if (status != 2) {
      throw new CustomException("您还不是管理员，无法查看申请信息！");
    }

    Page<ApplyResponseBody> pageInfo = applyService.getPage(page, pageSize);
    return R.success(pageInfo);
  }

  /**
   * 同意申请.
   *
   * @param id 申请编号
   * @return 申请处理结果
   */
  @PutMapping("/{id}")
  public R<String> agree(@PathVariable Long id) {
    int status = BaseContext.getCurrentUser().getStatus();
    if (status != 2) {
      throw new CustomException("您还不是管理员，无法处理申请！");
    }

    applyService.agree(id);
    return R.success("您已同意该申请");
  }

  /**
   * 拒绝申请.
   *
   * @param id 申请编号
   * @return 申请处理结果
   */
  @DeleteMapping("/{id}")
  public R<String> disagree(@PathVariable Long id) {
    int status = BaseContext.getCurrentUser().getStatus();
    if (status != 2) {
      throw new CustomException("您还不是管理员，无法处理申请！");
    }

    applyService.disagree(id);
    return R.success("您已拒绝该申请");
  }

  /**
   * 是否可申请.
   *
   * @return 布尔值
   */
  @GetMapping("/judge")
  public R<Boolean> judge() {
    Long currentId = BaseContext.getCurrentId();
    Boolean flag = applyService.judge(currentId);
    return R.success(flag);
  }
}
